// pages/car/car.js
const app = getApp()
const db = wx.cloud.database()
Page({
  data: {
    carts: [], // 购物车列表
    loadingHidden: false,
    hid: false
  },
  getTotalPrice() {
    var that = this //把页面的this赋给that
    let carts = that.data.carts; // 获取购物车列表
    let total = 0;
    for (let i = 0; i < carts.length; i++) { // 循环列表得到每个数据
      if (carts[i].selected) { // 判断选中才会计算价格
        total += carts[i].num * carts[i].price; // 所有价格加起来
      }
    }
    that.setData({
      carts: carts,
      totalPrice: total.toFixed(2)
    });
  },
  onShow() {
    var that = this;
    db.collection('testkg').doc('order')
      .get().then(res => {
        var hid = that.data.hid;
        var resHid = res.data.orderKg;
        if (resHid == true) {
          that.setData({
            hid: resHid
          })
        } else {
          var carts = wx.getStorageSync('carts');
          var total = 0;
          for (let i = 0; i < carts.length; i++) { // 循环列表得到每个数据
            if (carts[i].selected) { // 判断选中才会计算价格
              total += carts[i].num * carts[i].price; // 所有价格加起来
            }
          }
          that.setData({
            totalPrice: total.toFixed(2),
            carts: carts,
            loadingHidden: true
          })
        }
      })
  },
  selectList(event) {
    const index = event.currentTarget.dataset.index;
    let carts = this.data.carts;
    const selected = carts[index].selected;
    carts[index].selected = !selected;
    wx.setStorageSync('carts', carts);
    this.setData({
      carts: carts
    });
    this.getTotalPrice();
  },
  deleteList(e) {
    var carts = this.data.carts;
    const index = e.currentTarget.dataset.index;
    const name = this.data.carts[index].name;
    carts.splice(index, 1);
    this.setData({
      carts: carts
    })
    wx.setStorageSync('carts', carts);
    this.getTotalPrice();
  },
  addCount(e) {
    const index = e.currentTarget.dataset.index;
    const name = this.data.carts[index].name;
    var carts = this.data.carts;
    let num = carts[index].num;
    num = num + 1;
    carts[index].num = num;
    wx.setStorageSync('carts', carts);
    this.setData({
      carts: carts
    });
    this.getTotalPrice();
  },
  reduce(e) {
    const index = e.currentTarget.dataset.index;
    const name = this.data.carts[index].name;
    var carts = this.data.carts;
    let num = carts[index].num;
    if (num > 1) {
      num = num - 1;
      carts[index].num = num;
      wx.setStorageSync('carts', carts);
      this.setData({
        carts: carts
      })
    }
    this.getTotalPrice();
  },
  buy(event) {
    var a = this.data.carts;
    var newarr = [];
    for (let i = 0; i < a.length; i++) {
      if (a[i].selected) {
        newarr.push(a[i])
      }
    };
    let t4 = JSON.stringify(newarr);
    var t = this.data.totalPrice;
    if (t > 0) {
      wx.navigateTo({
        url: '/pages/order/order?id=' + JSON.stringify(newarr) /*t 为要传过页面的值*/
      })
    } else {
      wx.showToast({
        title: '金额等于0！！！',
        duration: 600,
        icon: 'error'
      })
    }
  },
})